package com.jokey.vue.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2021/11/25 20:36
 * @version: v1.0.0
 * <p>HR表</p>
 */
@Data
@TableName(value = "hr", autoResultMap = true)
public class Hr implements Serializable, UserDetails {

    /**
     * 主键ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 名称
     */
    private String name;

    /**
     * 手机号码
     */
    private String phone;

    /**
     * 住宅电话
     */
    private String telephone;

    /**
     * 联系地址
     */
    private String address;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 用户头像
     */
    private String userFace;

    /**
     * 备注
     */
    private String remark;

    /**
     * 是否可用
     *
     * 该字段在保存或更新时报错：Illegal overloaded getter method with ambiguous type for property 'enabled'
     * 主要原因是使用了@Data注解，并且Hr类实现@see org.springframework.security.core.userdetails.UserDetails,重写了isEnabled方法
     * 由于在此处isEnabled方法和getEnabled方法的逻辑完全一致，因此两者均被当作enabled属性的getter方法。
     * 在被不需要生成getter方法的属性上添加如下注解：@Getter(value = AccessLevel.NONE)
     */
    @Getter(value = AccessLevel.NONE)
    private Boolean enabled;

    /**
     * 拥有的角色
     */
    @TableField(exist = false)
    private List<Role> roles;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
